package com.csdj.t309.utils;

import com.mysql.cj.util.StringUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FileUtil {

    public static List<String> getFamliycsv(String path) {
        try (Reader in = new FileReader(path, Charset.forName("gbk"));) {
            final CSVFormat csvFormat = CSVFormat.Builder.create(CSVFormat.EXCEL).setHeader()
                    .setSkipHeaderRecord(true)
                    .build();
            List<String> list = new ArrayList<>();
            Iterable<CSVRecord> records = csvFormat.parse(in);
            records.forEach(item -> {
                String name = item.get("name");
                String tag = item.get("tag");
                if(StringUtils.isNullOrEmpty(name)){
                    return;
                }
                if("1".equals(tag)){
                    if(name.startsWith("?")){
                        name = name.substring(4,name.length());
                    }
                    for(String n: name.split("")){
                        list.add(n);
                    }
                }else{
                    if(name.startsWith("?")){
                        name = name.substring(4,name.length());
                    }
                    list.add(name);
                }
            });
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> getNames(){
        List<String> list = new ArrayList<>();
        try {
            FileReader reader = new FileReader("e:/csvtest/name2.txt");
            BufferedReader br = new BufferedReader(reader);
            br.lines().forEach(str ->{
                String[] split = str.split("、");
                for(String s:split){
                    if(!StringUtils.isNullOrEmpty(s)){
                        list.add(s);
                    }
                }
            });
            br.close();
            reader.close();
            BufferedReader br2 = new BufferedReader(new FileReader("e:/csvtest/name1.txt"));
            br2.lines().forEach(str -> {
                String[] split = str.split("，");
                for(String n:split){
                    if(!StringUtils.isNullOrEmpty(n)){
                        list.add(n);
                    }
                }
            });
            br2.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static String getName(){
        List<String> names = getNames();
        List<String> famliys = getFamliycsv("e:/csvtest/famliy-name.csv");
        int nameSize = names.size();
        int famliySize = famliys.size();
        Random random = new Random();
        return famliys.get(random.nextInt(famliySize))+names.get(random.nextInt(nameSize));
    }

    public static void main(String[] args) {
       List<String> names = getNames();
       List<String> famliys = getFamliycsv("e:/csvtest/famliy-name.csv");
       int nameSize = names.size();
       int famliySize = famliys.size();
        Random random = new Random();
        for (int i = 0; i < 1000000; i++) {
            System.out.println(famliys.get(random.nextInt(famliySize))+names.get(random.nextInt(nameSize)));
        }
    }
}
